package com.module.bag
{
	import com.connect.agreement.mclgc.Packet;
	
	import events.DragEvent;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.managers.DragManager;
	import mx.utils.ObjectProxy;
	
	import pops.BagBox;

	/**
	 * 用作处理所有显示物品图标的管理器
	 *  
	 * @author chzcb2008@gmail.com
	 * 
	 */
	public class BagManager
	{
		public static const GetUserItems:int = 1;
		public static const AddUserItems:int = 2;
		public static const DeleteUserItems:int = 3;
		
		private static var _instance:BagManager = new BagManager();
		public static function get instance():BagManager
		{
			return _instance;
		}
		
		[Bindable]
		public var bagitems:ArrayCollection = new ArrayCollection();
		
		private var itembox:BagBox;
		
		private var curbag:int = 1;
		/**
		 * 总格子数
		 */
		private var totalcount:int = 36;
		
		private var opencount:int = 16;
		
		public function BagManager()
		{
			for(var i:int=0;i<totalcount;i++){
				if(i<opencount){
					bagitems.addItem(new ObjectProxy({pos:i,lock:false,empty:true,data:null}));
				} else {
					bagitems.addItem(new ObjectProxy({pos:i,lock:true,empty:true,data:null}));
				}
			}
		}
		
		/**
		 * 设置背包 
		 * @param value 背包
		 * 
		 */
		public function setBag(value:BagBox):void
		{
			itembox = value;
		}
		
		public function get bag():BagBox
		{
			return itembox;
		}
		
		public function test():void
		{
			for each(var obj:Object in bagitems){
				if(obj.pos<5){
					obj.empty = false;
					obj.data = {source:'images/icon/10030.jpg'};
				}
			}
		}
		
		public function initBagPanel():void
		{
			
		}
		
		private function flushlist():void
		{
			
		}
		
		//---------------------------
		//组件的函数
		//---------------------------
		public function onDrag(event:MouseEvent):void
		{
			if(this.bag.list.selectedItem.lock || this.bag.list.selectedItem.empty){
				this.bag.list.dragEnabled = false;
				event.stopPropagation();
			}
			else
			{
				this.bag.list.dragEnabled = true;
			}
		}
		
		public function completedDrag(event:DragEvent):void
		{
			if(!event.source){//没有拖放到任何地方，判断是否扔掉
				
			}
		}
		
		public function dropDrag(event:DragEvent):void
		{
			var todropindex:int = this.bagitems.getItemIndex(event.source.data);
			if(todropindex>=this.bagitems.length){
				this.bag.list.selectedIndex = -1;
				return;
			}
			var fromindex:int = this.bag.list.selectedIndex;
			var toitem:Object = this.bagitems.getItemAt(todropindex);
			event.ownDo();
			if(toitem.lock)
			{
				return;
			}
			var fromitem:Object = this.bagitems.getItemAt(fromindex);
			this.bagitems.setItemAt(toitem,fromindex);
			this.bagitems.setItemAt(fromitem,todropindex);
			this.bagitems.refresh();
		}
		
		public function startDrag(event:DragEvent):void
		{
			
		}
		
		public function dragCheck(event:MouseEvent):void
		{
			
		}
		
		public function update(p:Packet):void
		{
			
		}
	}
}